if (typeof PIECESOFUK == "undefined") {
	var PIECESOFUK = {};
}

soundManager.url = 'Javascript/soundmanager2.swf'

soundManager.onload = function() {
// soundManager.createSound() etc. may now be called
 soundManager.createSound('WWSYS','Audio/WWSYS.mp3');
 soundManager.createSound('YWMFM','Audio/YWMFM.mp3');
 soundManager.createSound('FG','Audio/FG.mp3');
 soundManager.createSound('MS','Audio/MS.mp3');
 soundManager.createSound('H','Audio/Hands.mp3');
 soundManager.createSound('DSL','Audio/DSL.mp3');
 soundManager.createSound('J','Audio/Jupiter.mp3');
 soundManager.createSound('LU','Audio/LU.mp3');
 soundManager.createSound('WSIT','Audio/WSIT.mp3');
 soundManager.createSound('SS','Audio/SS.mp3');
 soundManager.createSound('BM','Audio/BM.mp3');
 soundManager.createSound('TW','Audio/TW.mp3');
 soundManager.createSound('STE','Audio/STE.mp3');
 soundManager.createSound('I','Audio/Intuition.mp3');
 soundManager.createSound('S','Audio/Stand.mp3');
 soundManager.createSound('2B1','Audio/2Become1.mp3');
 soundManager.createSound('AAA','Audio/AaA.mp3');
 soundManager.createSound('GD','Audio/GoodDay.mp3');
 soundManager.createSound('OOT','Audio/OOT.mp3');
 soundManager.createSound('SW','Audio/SW.mp3');
 soundManager.createSound('ID','Audio/IDo.mp3');
 soundManager.createSound('TIFLC','Audio/TIFLC.mp3');

}

var songIds = [ 'WWSYS', 'YWMFM', 'FG', 'MS', 'H', 'DSL', 'J', 'LU', 'WSIT', 'SS', 'BM',
                 'TW', 'STE', 'I', 'S', '2B1', 'AAA', 'GD', 'OOT', 'SW', 'ID', 'TIFLC'];

var songCovers = {
 "WWSYS" : "Data/Singles/WWSYSSmall.jpg",
 "YWMFM" : "Data/Singles/YWMFMSmall.jpg",
 "FG" : "Data/Singles/FoolishGamesSmall.jpg",
 "MS" : "Data/Singles/MorningSongSmall.jpg",
 "H" : "Data/Singles/HandsSmall.jpg",
 "DSL" : "Data/Singles/DownSoLongSmall.jpg",
 "J" : "Data/Singles/JupiterSmall.jpg",
 "LU" : "Data/Singles/LifeUncommonSmall.jpg",
 "WSIT" : "Data/Singles/WSITSmall.jpg",
 "SS" : "Data/Singles/StandingStillSmall.jpg",
 "BM" : "Data/Singles/BreakMeSmall.jpg",
 "TW" : "Data/Singles/ThisWaySmall.jpg",
 "STE" : "Data/Singles/ServeTheEgoSmall.jpg",
 "I" : "Data/Singles/IntuitionSmall.jpg",
 "S" : "Data/Singles/StandSmall.jpg",
 "2B1" : "Data/Singles/2Become1Small.jpg",
 "AAA" : "Data/Singles/AgainAndAgainSmall.jpg",
 "GD" : "Data/Singles/GoodDaySmall.jpg",
 "OOT" : "Data/Singles/OnlyOneTooSmall.jpg",
 "SW" : "Data/Singles/StrongerWomanSmall.jpg",
 "ID" : "Data/Singles/IDoSmall.png",
 "TIFLC" : "Data/Singles/TIFLCSmall.jpg"
};

  var numclicked = 0;
  var $firstClicked = null;
  var firstId = null;
  var $this = null;
  var theId = null;
  var startTime = new Date();

	var size;
	var matched = 0;
	var numSongs;
	var timer = null;

$(document).ready( function() {
/*
  $.ajax({
    type: "GET",
		async: true,
    url: "Data/singles.js",
    dataType: "json",
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log("Error reading Data/singles.js:" + textStatus);
		},
		success: function (data, textStatus) {
      for ( var i in data ) {
        var init = $.map( i.split(" "), function(s, j) { return s.slice(0, 1) } ).join("");

      }
    }
  });
*/
  time = setTimeout(updateTimer, 700);
  var numRows = 6;
  var numCols = 7;
  var numRows = 4;
  var numCols = 4;
	/*
  var numRows = 2;
  var numCols = 2;
	*/
  numSongs = numRows * numCols / 2;


  for (var i = 0; i < songIds.length; i++) {
    var temp = songIds[i];
    var swap = Math.floor(Math.random()* songIds.length);
    songIds[i] = songIds[swap];
    songIds[swap] = temp;
  }

  var songsMap = new Array();
  var count = 0;
  for (var i = 0; i < numSongs; i++) {
    songsMap[count++] = songIds[i]
    songsMap[count++] = songIds[i]
  }
  for (var i = 0; i < songsMap.length; i++) {
    var temp = songsMap[i];
    var swap = Math.floor(Math.random()* songsMap.length);
    songsMap[i] = songsMap[swap];
    songsMap[swap] = temp;
  }
//  alert( songsMap.join());
  size = Math.floor( Math.min( ( $(document).height() - 40 ) / numRows , $(document).width() / numCols  ));

  var idx = 0;
  for (var row = 0; row < numRows; ++row) {
    for (var col = 0; col < numCols; ++col) {
			var $img = $("<img>")
				.attr("src", songCovers[songsMap[idx]])
				.css("width", size + "px")
				.css("height", size + "px").hide();
			$("<div>")
			  .append($img)
				.data("songId", songsMap[idx])
				.css("width", size + "px")
				.css("height", size + "px")
				.addClass("aSquare")
				.addClass("unselected")
				.appendTo( $("#pairs") )	;
			++idx;
    }
		$("#pairs").append("<br clear='left'>");
  }
  $(".unselected").click( handleClick );
});

function updateTimer() {
  var now = new Date();
  var secs = Math.floor ( (now - startTime) / 1000);
  var mins = Math.floor(secs/60);
  var secs = secs % 60;
  $("#timeSecs").text(secs < 10 ? "0" + secs : secs);
  $("#timeMins").text(mins);
  timer = setTimeout(updateTimer, 700);
}

function doFinish() {
	clearTimeout(timer);
	var now = new Date();
  var secs = Math.floor ( (now - startTime) / 1000);
  var mins = Math.floor(secs/60);
  var secs = secs % 60;
  $("#timeSecs").text(secs < 10 ? "0" + secs : secs);
  $("#timeMins").text(mins);
  alert("Well Done! You finished in " + mins + " minutes, " + secs + " seconds");
}
function handleClick() {
  $(".unselected").unbind( "click");
  $this = $(this);
  $this.removeClass("unselected").addClass("selected");
  theId = $this.data("songId");

  soundManager.play(theId, {onfinish: function(){
    if (numclicked == 0) {
      $firstClicked = $this;
      firstId = theId;
      ++numclicked;
    } else {
      if ( theId && theId == firstId) {

/*
        $this.addClass("matched").unbind("click");
        $firstClicked.addClass("matched").unbind("click");
*/
        $this.addClass("matched");
        $firstClicked.addClass("matched");

		    $this.find("img").show();
		    $firstClicked.find("img").show();
				$("#matched").text(++matched);
				if ( matched == numSongs ) doFinish();
      } else {
        $this.addClass("unselected");
        $firstClicked.addClass("unselected");
      }
      $firstClicked.removeClass("selected");
      $this.removeClass("selected");

      $firstClicked = null;
      firstId = null;
      numclicked = 0;
    }
      $(".unselected").click( handleClick );
  }});

}
